package com.alvis.media.controller;

import com.alvis.media.base.BaseApiController;
import com.alvis.media.base.NonStaticResourceHttpRequestHandler;
import com.alvis.media.base.RestResponse;
import com.alvis.media.viewmodel.video.*;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController("ApiVideoController")
@RequestMapping(value = "/api/video")
@AllArgsConstructor
public class VideoController  extends BaseApiController {


    @Autowired
    private NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;
    //视频播放 ：playVideo方法实现视频流播放功能，从请求URL中提取视频文件路径，使用Java NIO的Files工具类检查文件存在性和获取文件MIME类型，通过NonStaticResourceHttpRequestHandler处理视频流传输。

    @GetMapping("/play/**/*")
    public void playVideo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //realPath 即视频所在的完整地址
        String url = request.getRequestURL().toString();
        int index=  url.lastIndexOf("D:");
        String realPath = url.substring(index);

        Path filePath = Paths.get(realPath);
        if (Files.exists(filePath)) {
            // 利用 Files.probeContentType 获取文件类型
            String mimeType = Files.probeContentType(filePath);
            if (!StringUtils.isEmpty(mimeType)) {
                // 设置 response
                response.setContentType(mimeType);
            }
            request.setAttribute(nonStaticResourceHttpRequestHandler.filepath, filePath);
            // 利用 ResourceHttpRequestHandler.handlerRequest() 实现返回视频流
            nonStaticResourceHttpRequestHandler.handleRequest(request, response);
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        }
    }

    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<VideoResponseVM> select(@PathVariable Integer id) {

        return null;
    }

    @RequestMapping(value = "/getVideoDetailByVideoId/{id}", method = RequestMethod.POST)
    public RestResponse <VideoDetailVM> getVideoDetailByVideoId(@PathVariable Integer id) {

        return null;
    }
    //视频列表查询 ：通过pageList方法实现分页查询，支持各种筛选条件。方法调用VideoInfoService获取原始数据，然后通过PageInfoHelper工具将实体对象转换为响应视图模型，保证了数据传输的安全性。//
    @RequestMapping(value = "/page/list", method = RequestMethod.POST)
    public RestResponse<PageInfo <VideoResponseVM>> pageList(@RequestBody VideoPageRequestVM model) {
        return null;
    }
    //视频添加 ：createVideo方法实现视频资源创建，包含完善的参数校验机制，确保视频文件已上传、分类和名称不为空。系统自动记录创建者信息和创建时间，并对视频URL进行格式化处理，按照日期组织存储结构。
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public RestResponse<VideoResponseVM> createVideo(@RequestBody @Valid VideoCreateVM model) {

        return RestResponse.ok();
    }

    //视频编辑 ：editVideo方法支持更新已有视频信息，先验证视频ID有效性和视频存在性，再执行更新操作并记录最后修改时间。如果提供了新的视频URL，则同步更新URL信息。
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse<VideoResponseVM> editVideo(@RequestBody @Valid VideoCreateVM model) {
        return RestResponse.ok();
    }
   // 删除 ：delete方法实现视频资源删除，包含前置检查确保要删除的视频存在，遵循先检查后操作的安全原则。
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        return RestResponse.ok();
    }


    @RequestMapping(value = "/userAnalysis/{id}", method = RequestMethod.POST)
    public RestResponse <UserAnalysisVM> userAnalysis(@PathVariable Integer id) {

        UserAnalysisVM vm = new UserAnalysisVM();

        return RestResponse.ok(vm);

    }
}
